package com.sijiao.difflog.util;

import com.sijiao.difflog.constants.ClassTypeEnum;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class ClassUtil {

    public static ClassTypeEnum getClassType(Object obj) {
        if (obj == null) {
            return ClassTypeEnum.none;
        }
        Class<?> clazz = obj.getClass();
        if (isPrimitive(clazz)) {
            return ClassTypeEnum.primitive;
        }
        if (clazz == String.class) {
            return ClassTypeEnum.string;
        }
        if (obj instanceof List) {
            return ClassTypeEnum.list;
        }
        if (obj instanceof Set) {
            return ClassTypeEnum.list;
        }
        if (obj instanceof Map) {
            return ClassTypeEnum.map;
        }
        return ClassTypeEnum.object;
    }

    public static ClassTypeEnum getClassType(Class<?> clazz) {
        if (clazz == null) {
            return ClassTypeEnum.none;
        }
        if (isPrimitive(clazz)) {
            return ClassTypeEnum.primitive;
        }
        if (clazz == String.class) {
            return ClassTypeEnum.string;
        }
        if (clazz == List.class) {
            return ClassTypeEnum.list;
        }
        if (clazz == Set.class) {
            return ClassTypeEnum.list;
        }
        if (clazz == Map.class) {
            return ClassTypeEnum.map;
        }
        return ClassTypeEnum.object;
    }

    private static boolean isPrimitive(Class<?> clazz) {
        return clazz == Byte.class
                || clazz == Short.class
                || clazz == Boolean.class
                || clazz == Character.class
                || clazz == Integer.class
                || clazz == Long.class
                || clazz == Float.class
                || clazz == Double.class;
    }

    public static void main(String[] args) {
        Integer aaa = Integer.valueOf(1000);
        System.out.println(getClassType(aaa));
        char ccc = 100;
        System.out.println(getClassType(ccc));
        String ss = "111";
        System.out.println(getClassType(ss));
    }
}
